
this.name <- "test_coin"
source("~/workspace/navi-motion/R/jhkim/base/util_include.R")
tbl <- read.csv("/home/jhkim/workspace/coin/python/bookdump.csv")
n <- NROW(tbl)
m <- 30
tbl$Y.bret <- c(tbl$bid0_price[(1+m):n] - tbl$ask0_price[1:(n-m)], rep(0, m))
tbl$Y.dura <- c(tbl$timestamp[(1+m):n] - tbl$timestamp[1:(n-m)], rep(0, m))

print(summary(tbl$Y.dura))
print(sprintf("%.4f hours", ((tbl$timestamp[n] - tbl$timestamp[1]) * 1e-9) / 3600))
#tbl$X.bpastret <- c(0, diff(tbl$bid0_price))
#tbl$X.apastret <- c(0, diff(tbl$ask0_price))
tbl$X.bpress <- tbl$ask0_qty / (tbl$ask0_qty + tbl$bid0_qty)
tbl$X.a01234 <- tbl$ask0_qty + tbl$ask1_qty + tbl$ask2_qty + tbl$ask3_qty + tbl$ask4_qty
tbl$X.b01234 <- tbl$bid0_qty + tbl$bid1_qty + tbl$bid2_qty + tbl$bid3_qty + tbl$bid4_qty
tbl$X.spread <- tbl$ask0_price - tbl$bid0_price

kEpsPrice <- 0.5

cask_taken <- function(past, new) {
  return (past$cross_ask0_qty * (past$cross_ask0_price <= new$cross_bid0_price + kEpsPrice) + past$cross_ask1_qty * (past$cross_ask1_price <= new$cross_bid0_price + kEpsPrice))
}

cbid_taken <- function(past, new) {
  return (past$cross_bid0_qty * (past$cross_bid0_price + kEpsPrice >= new$cross_ask0_price) + past$cross_bid1_qty * (past$cross_bid1_price + kEpsPrice >= new$cross_ask0_price))
}

cbid_miss <- function(past, new) {
  return (past$cross_bid0_qty * (past$cross_ask0_price <= new$cross_bid0_price + kEpsPrice) + past$cross_bid1_qty * (past$cross_ask0_price <= new$cross_bid1_price + kEpsPrice))
}

cask_miss <- function(past, new) {
  return (past$cross_ask0_qty * (past$cross_bid0_price + kEpsPrice >= new$cross_ask0_price) + past$cross_ask1_qty * (past$cross_bid0_price + kEpsPrice >= new$cross_ask1_price))
}

ask_taken <- function(past, new) {
  return (past$ask0_qty * (past$ask0_price <= new$bid0_price + kEpsPrice) + past$ask1_qty * (past$ask1_price <= new$bid0_price + kEpsPrice))
}

bid_miss <- function(past, new) {
  return (past$bid0_qty * (past$ask0_price <= new$bid0_price + kEpsPrice) + past$bid1_qty * (past$ask0_price <= new$bid1_price + kEpsPrice))
}

bid_taken <- function(past, new) {
  return (past$bid0_qty * (past$bid0_price + kEpsPrice >= new$ask0_price) + past$bid1_qty * (past$bid1_price + kEpsPrice >= new$ask0_price))
}

ask_miss <- function(past, new) {
  return (past$ask0_qty * (past$bid0_price + kEpsPrice >= new$ask0_price) + past$ask1_qty * (past$bid0_price + kEpsPrice >= new$ask1_price))
}

time_diff <- function(past, new) { return (-past$timestamp + new$timestamp) }
ask_ret <- function(past, new) { return (-past$ask0_price + new$ask0_price) }
bid_ret <- function(past, new) { return (-past$bid0_price + new$bid0_price) }
cask_ret <- function(past, new) { return (-past$cross_ask0_price + new$cross_ask0_price) }
cbid_ret <- function(past, new) { return (-past$cross_bid0_price + new$cross_bid0_price) }

options(width=160)

for (colname in c(
    "ask_taken", "bid_miss", "bid_taken", "ask_miss", "time_diff",
    "ask_ret", "bid_ret",
    "cask_taken", "cbid_taken", "cask_miss", "cbid_miss",
    "cask_ret", "cbid_ret"
    )) {
  for (w in c(10, 5)) {
    colname_x <- sprintf("X.%s_%s", colname, w)
    feature <- get(colname)
    tbl[(1+w):n, colname_x] <- feature(tbl[1:(n-w), ], tbl[(1+w):n, ])
  }
}

tbl$cross_ask_diff <- tbl$cross_ask0_price - tbl$ask0_price
tbl$cross_bid_diff <- tbl$cross_bid0_price - tbl$bid0_price

tbl$X.cross_bpress <- tbl$cross_bid0_qty / (tbl$cross_bid0_qty + tbl$cross_ask0_qty)
tbl$X.cross_a01234 <- tbl$cross_ask0_qty + tbl$cross_ask1_qty + tbl$cross_ask2_qty + tbl$cross_ask3_qty + tbl$cross_ask4_qty
tbl$X.cross_b01234 <- tbl$cross_bid0_qty + tbl$cross_bid1_qty + tbl$cross_bid2_qty + tbl$cross_bid3_qty + tbl$cross_bid4_qty

for (w in c(30, 120)) {
  tbl[(1+w):n, sprintf("X.ask_basis_ret_%s", w)] <- tbl[1:(n-w), "cross_ask_diff"] - tbl[(1+w):n, "cross_ask_diff"]
  tbl[(1+w):n, sprintf("X.bid_basis_ret_%s", w)] <- tbl[1:(n-w), "cross_bid_diff"] - tbl[(1+w):n, "cross_bid_diff"]
}

#tbl <- tbl[seq(1,NROW(tbl),10), ]
print(NROW(tbl))
n <- NROW(tbl)
m <- 30

for (colname in grep("X.", names(tbl), value=TRUE)) {
  print(colname)
  print(GetQuantile(tbl$Y.bret, tbl[, colname], 10))
  png(sprintf("/home/jhkim/Documents/bitmex_test/%s.png", colname))
  PlotQuantileSqueeze(tbl$Y.bret, tbl[, colname], colname)
  dev.off()
}

cols <- c(grep("X.", names(tbl), value=TRUE), "Y.bret")
tblin <- tbl[1:(n/2), cols]
tblout <- tbl[(n/2):n, cols]

model <- lm('Y.bret ~ .', tblin)
print(summary(model))
yin <- predict(model, tblin)
yout <- predict(model, tblout)
print(GetQuantile(tblin$Y.bret, yin, 10))
print(GetQuantile(tblout$Y.bret, yout, 10))
